<?php
namespace App\Services\Impl;
use App\Models\Push;
use App\Services\PushServiceInterFace;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redis;
use App\Constant\ApiUrl;
use Illuminate\Support\Facades\Log;
use App\Exceptions\BusinessException;
final class PushServiceImpl implements PushServiceInterFace{
    public function getQrCode(){
        $accessKey = Redis::get('push_access_key_'.Auth::id());
        if (is_null($accessKey)) {
            $accessKey = $this->getAccessKey();
        }
        $header = [
                'access-key' => $accessKey,
        ];
        try {
            $response = Http::withHeaders($header)->get(ApiUrl::getQrCode());
            if ($response['code']== 200){
                Log::info(ApiUrl::getQrCode());
               return $response;
            }else{
                throw new BusinessException($response['msg']) ;
            }
        } catch (\Throwable $th) {
            throw new BusinessException("请求超时") ;
        }
        
    }

    public function getWechatQrCode(){
        $response = Http::get(ApiUrl::getWechatQrcode());
        if ($response['code']== 200){
            return $response;
        }else{
            throw new BusinessException($response['msg']) ;
        }
    }
     public function getfriendlist($data){
        $accessKey = Redis::get('push_access_key_'.Auth::id());
        if (is_null($accessKey)) {
            $accessKey = $this->getAccessKey();
        }
        $header = [ 
            'access-key' => $accessKey,
        ];
        $requestBody = [
            "current"=>$data['current'],
            "pageSize"=> $data['pageSize']
        ];
        $response = Http::withHeaders($header)->post(ApiUrl::getFriendList(), $requestBody);
        if ($response['code']== 200){
            return $response['data'];
        }else{
            throw new BusinessException($response['msg']) ;
        }
    }
    public function delfriend(){
        
    }

    public function getAccessKey(){
        $accessKey = Redis::get('push_access_key_'.Auth::id());
        if (!is_null($accessKey)) {
            return $accessKey;
        }

        $requestBody = [
            "token"=> env('PUSH_TOKEN'),
            "secretKey"=> env('PUSH_SECURITY'),
        ];
        try {
            $response = Http::post(ApiUrl::getPushAccessKey(), $requestBody);
            if ($response['code']== 200){
                $accessKey = $response['data']['accessKey'];
                // 存储到Redis,设置2小时过期
                Redis::setex('push_access_key_'.Auth::id(), 7200, $accessKey);
                return $accessKey;
            }else{
                throw new BusinessException($response['msg']) ;
            }
            
        } catch (\Throwable $th) {
            throw new BusinessException("请求超时") ;
        }
    }

    public function sendMsg($data){
        $requestBody = [
            "token"=> $data['token'],
            "msg"=> $data['msg'],
        ];
        try{
            $response = Http::get(ApiUrl::sendMsg($requestBody));
       }catch(\Throwable $th){
        throw new BusinessException("请求超时 10s后再重试");
       }
        return $response;
    }

    public function sendTestMsg($token, $content){
        $requestBody = [
            "token"=> $token,
            "msg"=> $content,
        ];
       try{
            $response = Http::get(ApiUrl::sendMsg($requestBody));
       }catch(\Throwable $th){
        throw new BusinessException("请求超时 10s后再重试");
       }
        return $response;
    } 

    public function editFriendDetails($data){
        $accessKey = Redis::get('push_access_key_'.Auth::id());
        if (is_null($accessKey)) {
            $accessKey = $this->getAccessKey();
        }
        $requestBody = [
            "id"=> $data['id'],
            "remark"=> $data['remark']
        ];
        $header = [
            'access-key' => $accessKey,
        ];
        Log::info( $requestBody);
        Log::info( $header);
        $response = Http::withHeaders($header)->post(ApiUrl::editRemark(), $requestBody);
        return $response->json();
    }
    public function deleteFriendDetails($data){
        $accessKey = Redis::get('push_access_key_'.Auth::id());
        if (is_null($accessKey)) {
            $accessKey = $this->getAccessKey();
        }
        $header = [
            'access-key' => $accessKey,
        ];
        $response = Http::withHeaders($header)->get(ApiUrl::deleteFriend($data['friendId']));
        
        if($response->json()['code'] === 200){
           Push::where('token', $data['token'])->delete();
        }
        return $response->json();
    }
}